package study.async.springboot.support.async;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * （1）使用 spring 提供的执行器 ThreadPoolTaskExecutor
 */
@Slf4j
@Configuration
public class _1_AsyncConfig {

    @Bean("executor_1")
    public Executor executor01() {
        //spring 自定义执行器
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //thread name prefix
        executor.setThreadNamePrefix("spring-pool-thread-");
        //thread size
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(5);
        //queue size
        executor.setQueueCapacity(0);
        //keep alive
        executor.setKeepAliveSeconds(5);
        //reject handler
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

        return executor;
    }
}
